/*******************************************************************************
 * Copyright（C） CETC-32
 * @file:LoginUserResolver.java
 * @Description：
 * @Author ：徐文远
 * @version：1.0
 * @date ： 2021/3/9 上午9:50
 ******************************************************************************/

package com.cetc32.webutil.client.resolvers;

import com.cetc32.webutil.common.bean.LoginUser;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class LoginUserResolver implements HandlerMethodArgumentResolver {
    @Override
    public boolean supportsParameter(MethodParameter methodParameter) {
        Class<?> clazz =methodParameter.getParameterType();
        return clazz== LoginUser.class;
    }

    @Override
    public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer,
                                  NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {
        HttpServletRequest request=nativeWebRequest.getNativeRequest(HttpServletRequest.class);
        HttpServletResponse response=nativeWebRequest.getNativeResponse(HttpServletResponse.class);
        LoginUser user = (LoginUser) request.getSession().getAttribute("loginUser");
        return user;
    }
}
